<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end">
          <el-button type="primary" @click="addPerm('0',1)"><i class="el-icon-plus" />新增权限</el-button>
        </el-row>
        <el-table border :data="premList" row-key="id" default-expand-all>
          <el-table-column align="center" label="名称" prop="name" />
          <el-table-column align="center" label="标识" prop="code" />
          <el-table-column align="center" label="描述" prop="description" />
          <el-table-column align="center" label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" type="text" @click="addPerm(row.id,2)">添加</el-button>
              <el-button type="text" @click="editPermFn(row.id)">编辑</el-button>
              <el-button type="text" @click="removeFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog :title="`${showText}权限`" :visible="showDialog" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermission, removePerm, addPerm, getPremById, editPermById } from '@/api/permission'
import { updateDepartmentList } from '@/utils/index'
export default {
  data() {
    return {
      premList: [],
      showDialog: false,
      formData: {
        enVisible: '1',
        name: '',
        code: '',
        description: '',
        type: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPremission()
  },
  methods: {
    async getPremission() {
      const res = await getPermission()
      const data = updateDepartmentList(res, '0')
      this.premList = data
      console.log(data)
    },
    async removeFn(id) {
      await this.$confirm('确认删除？')
      await removePerm(id)
      this.$message.success('删除成功')
      this.getPremission()
    },
    addPerm(id, type) {
      this.showDialog = true
      this.formData.pid = id
      this.formData.type = type
    },
    async editPermFn(id) {
      this.formData = await getPremById(id)
      this.showDialog = true
    },
    async btnOK() {
      await this.$refs.perForm.validate()
      if (this.formData.id) {
        await editPermById(this.formData)
      } else {
        await addPerm(this.formData)
      }
      this.$message.success('操作成功')
      this.getPremission()
      this.showDialog = false
    },
    btnCancel() {
      this.formData = {
        enVisible: '1',
        name: '',
        code: '',
        description: '',
        type: '',
        pid: ''
      }
      this.$refs.perForm.resetFields()
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
